﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WCFVeterinaria;

public partial class VerConsultaMedica : System.Web.UI.Page
{
    ServiceVeterinaria vet = new ServiceVeterinaria();
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            CargarCitas();
        }
    }

    private void CargarCitas()
    {
        ConsultasGridView.DataSource = vet.listarConsultaMedica();
        ConsultasGridView.DataBind();
    }

    protected void ConsultasGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType != DataControlRowType.DataRow)
            return;


        DataRowView item = (DataRowView)(e.Row.DataItem);

        string idSolicitud = Convert.ToString(item.Row["consultaID"]);
        WCFVeterinaria.Animal animal = new WCFVeterinaria.Animal();
        int animalID=Convert.ToInt32(item.Row["animalID"]);
        animal = vet.consultaAnimal(animalID);

        WCFVeterinaria.Medicos medico = new WCFVeterinaria.Medicos();
        int numColegiado = Convert.ToInt32(item.Row["nro_colegiado"]);
        medico = vet.consultarMedico(numColegiado);

        // enlace al Detalle
        HyperLink detalleSolicitudHyperLink = (HyperLink)e.Row.FindControl("ConsultaHyperLink");
        detalleSolicitudHyperLink.Text = idSolicitud;

        Label animalLabel = (Label)e.Row.FindControl("AnimalLabel");
        animalLabel.Text = animal.Nombre;

        Label medicoLabel = (Label)e.Row.FindControl("MedicoLabel");
        medicoLabel.Text = medico.Nombre + " " + medico.Apellidos;

        detalleSolicitudHyperLink.NavigateUrl = string.Format(
           "/ConsultaMedica.aspx?idCita={0}",
           idSolicitud
           );
    }
    protected void ConsultasGridView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        LinkButton lnkBtn = (LinkButton)e.CommandSource;    // the button
        GridViewRow myRow = (GridViewRow)lnkBtn.Parent.Parent;  // the row
        switch (e.CommandName.ToLower())
        {
            case "delete":
                GridView myGrid = (GridView)sender; // the gridview
                int index = (int)myGrid.DataKeys[myRow.RowIndex].Value; // value of the datakey 
                vet.eliminarConsultaMedica(index);
                break;
        }
    }
    protected void ConsultasGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        CargarCitas();
    }
}